//
//  SEPrescriptionNewSectionHeader.swift
//  SmartEyeForDoctor
//
//  Created by TT on 2021/5/10.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

enum SEPrescriptionNewSectionHeaderType: Int {
    case templateName, mainDiagnosis, subDiagnosis, children, prescription
    
    var title: String {
        switch self {
        case .templateName:  return "模板名称"
        case .mainDiagnosis: return "主诊断"
        case .subDiagnosis:  return "次诊断"
        case .children:      return "是否为儿童处方"
        case .prescription:  return "处方单"
        }
    }
    
    var rightBtnHidden: Bool {
        switch self {
        case .templateName,
             .mainDiagnosis: return true
        case .subDiagnosis,
             .children,
             .prescription:  return false
        }
    }
    
    var textFieldHidden: Bool {
        switch self {
        case .templateName: return false
        default:            return true
        }
    }
}

class SEPrescriptionNewSectionHeader: UIView, UITextFieldDelegate {
    
    var rightButtonHandler: ((SEPrescriptionNewSectionHeaderType) -> Void)?
    var textFieldDidEndEdit: ((String) -> Void)?
    
    private var headerType: SEPrescriptionNewSectionHeaderType = .mainDiagnosis
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    @IBOutlet private weak var rightButton: UIButton!
    
    @IBOutlet private weak var textField: UITextField! {
        didSet {
            textField.delegate = self
        }
    }
    
    @IBAction private func rightButtonAction() {
        rightButtonHandler?(headerType)
    }
    
    @objc private func pickTemplateTapped() {
        rightButtonHandler?(headerType)
    }
    
    func update(with viewModel: SEPrescriptionViewModel, headerType: SEPrescriptionNewSectionHeaderType, clickHandler: ((SEPrescriptionNewSectionHeaderType) -> Void)?) {
        self.headerType = headerType
        self.rightButtonHandler = clickHandler
        
        titleLabel.text = headerType.title
        rightButton.isHidden = viewModel.canEdit ? headerType.rightBtnHidden : true
        textField.isHidden = headerType.textFieldHidden
        
        switch headerType {
        case .subDiagnosis, .prescription:
            rightButton.setTitle(nil, for: .normal)
            rightButton.setImage(Asset.iconAdd.image, for: .normal)
            rightButton.setImage(Asset.iconAdd.image, for: .highlighted)
        case .children:
            rightButton.isHidden = false
            rightButton.isEnabled = viewModel.canEdit
            rightButton.setTitle(viewModel.isChildrenStr, for: .normal)
            rightButton.setImage(Asset.arrowRightIcon.image, for: .normal)
            rightButton.setImage(Asset.arrowRightIcon.image, for: .highlighted)
            rightButton.layoutButton(with: .imageRight)
        case .templateName:
            textField.text = viewModel.prescription.name
            switch viewModel.prescriptionMode {
            case let .prescription(_, _, isNew):
                textField.isUserInteractionEnabled = false
                if isNew || viewModel.prescription.status.canEdit {
                    let tap = UITapGestureRecognizer(target: self, action: #selector(pickTemplateTapped))
                    addGestureRecognizer(tap)
                }
            case .template:
                textField.isUserInteractionEnabled = true
            }
        default: break
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        guard let text = textField.text, !text.isEmpty else { return }
        textFieldDidEndEdit?(text)
    }
}
